import logging
import sys
import os
# import app_ui
from ui import MainApplication
import signal


#强制指定使用PiGPIOFactory并配置服务地址
# from gpiozero import Device
# from gpiozero.pins.pigpio import PiGPIOFactory
 
# # 配置pigpio服务地址和端口（根据实际环境修改）
# PIGPIO_ADDR = os.getenv('PIGPIO_ADDR', 'localhost')  # 默认localhost
# PIGPIO_PORT = int(os.getenv('PIGPIO_PORT', '8888'))  # 默认8888
 
# # 显式设置Pin Factory
# Device.pin_factory = PiGPIOFactory(host=PIGPIO_ADDR, port=PIGPIO_PORT)
 
# 后续正常编写业务代码...


# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(threadName)s] %(message)s",
    handlers=[logging.StreamHandler(sys.stdout)]
)
def signal_handler(sig, frame):
    logging.warning("捕获到Ctrl+C信号")
    app.safe_shutdown()

if __name__ == "__main__":
    app = MainApplication()
    # 设置Ctrl+C信号处理
    try:
        signal.signal(signal.SIGINT, signal_handler)
    except ImportError:
        pass  # Windows系统可能需要额外的处理
    # 运行主循环
    app.mainloop()
    # 确保完全退出
    os._exit(0)